package com.gj.costprice.service.impl;

import com.yyq.costprice.EnumPackage.CurrencyEnum;
import com.yyq.costprice.EnumPackage.StatusEnum;
import com.yyq.costprice.mapper.CostPriceMapper;
import com.yyq.costprice.model.*;
import com.yyq.costprice.service.CostPriceService;
import com.yyq.util.AllRecords;
import com.yyq.util.ResultBuilderVo;
import com.yyq.util.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2021-06-28.
 */
@Service
public class CostPriceServiceImpl implements CostPriceService {

    @Autowired
    private CostPriceMapper costPriceMapper;

    @Override
    public ResultVo queryPage(@Valid CostPriceSelectVo costPriceSelectVo) {
     //查询所有数据
        List<CostPriceVo> costPriceVoList = costPriceMapper.queryByPage(costPriceSelectVo);
    //查询价格分类
        List<PriceClassVo> priceClassVoList = costPriceMapper.queryPriceClass();
        //查询税率
        List<Taxtype> taxtypes = costPriceMapper.queryAllTaxType();
        //添加name值
        for (CostPriceVo costPriceVo:costPriceVoList) {
            //启用禁用
            costPriceVo.setStatusName(StatusEnum.getStatusName(costPriceVo.getStatus()));
            //币别
            costPriceVo.setCurrencyCodeName(CurrencyEnum.getcurrencyCodeName(costPriceVo.getCurrencyCode()));
            //税率
            for (Taxtype taxtype:taxtypes) {
                if (taxtype.getTaxType()==costPriceVo.getTaxType()){
                    costPriceVo.setTaxDec(taxtype.getTaxDec());
                }
            }
            //价格分类
            for (PriceClassVo priceClassVo:priceClassVoList) {
                if (priceClassVo.getPriceType()==costPriceVo.getPriceType()){
                    costPriceVo.setPriceTypeName(priceClassVo.getPriceTypeName());
                }
            }
        }
    //统计数量
        int count = costPriceMapper.count(costPriceSelectVo);

        AllRecords allRecords = new AllRecords();
        allRecords.setPageIndex(costPriceSelectVo.getPageIndex());
        allRecords.setPageSize(costPriceSelectVo.getPageSize());
        allRecords.setTotalNumber(count);
        allRecords.setDataList(costPriceVoList);
        allRecords.resetTotalNumber(count);

        return ResultBuilderVo.success(allRecords);
    }

    @Override
    public ResultVo add(@Valid CostPriceAddVo costPriceAddVo) {
        //查看是否有重复数据
       int count =  costPriceMapper.queryByCode(costPriceAddVo.getLegalPlantCode(),costPriceAddVo.getMaterialCode());
       if(count!=0){
           //进行判断


           return ResultBuilderVo.error("有重复数据，新增失败！");
       }else if(count==0) {//没有重复数据

           CostPriceVo costPriceVo = new CostPriceVo();
           BeanUtils.copyProperties(costPriceAddVo, costPriceVo);
           Date now = new Date();
           costPriceVo.setStatus(1);
           costPriceVo.setCreatorName("yyq");
           costPriceVo.setUpdateName("yyq");
           costPriceVo.setCreateTime(now);
           costPriceVo.setUpdateTime(now);
           int a = costPriceMapper.add(costPriceVo);
           return ResultBuilderVo.success();
       }
        return ResultBuilderVo.error("新增失败");
    }

    @Override
    public ResultVo queryPlantListByLegalPersonCode(String legalPersonCode) {
        List<String> list = costPriceMapper.queryPlantListByLegalPersonCode(legalPersonCode);
        return ResultBuilderVo.success(list);
    }

    @Override
    public ResultVo getPullDownList() {

        //价格分类
        List<PriceClassVo> priceClassVoList = costPriceMapper.queryPriceClass();
        //币别
        List<CurrencyVo> currencyVoList = new ArrayList<>();
        for (CurrencyEnum c: CurrencyEnum.values()) {
            CurrencyVo currencyVo = new CurrencyVo();
            currencyVo.setCurrencyCode(c.getcurrencyCode());
            currencyVo.setCurrencyCodeName(c.getcurrencyCodeName());
            currencyVoList.add(currencyVo);
        }
        //当前状态
        List<StatusVo> statusVoList = new ArrayList<>();
        for (StatusEnum c: StatusEnum.values()) {
            StatusVo statusVo = new StatusVo();
            statusVo.setStatus(c.getStatus());
            statusVo.setStatusName(c.getStatusName());
            statusVoList.add(statusVo);
        }

        HashMap<String,Object> map = new HashMap();
        map.put("materialCostPrice.priceType",priceClassVoList);
        map.put("currencyCode",currencyVoList);
        map.put("materialCostPrice.materialCostPriceStatus",statusVoList);
        return ResultBuilderVo.success(map);
    }

    @Override
    public ResultVo queryAllTaxType() {
        List<Taxtype> taxType = costPriceMapper.queryAllTaxType();
        return ResultBuilderVo.success(taxType);
    }

    @Override
    public ResultVo searchEnabled(@Valid SparePartVo sparePartVo) {
        List<SparePartVo> sparePartVoList = costPriceMapper.page1(sparePartVo);
        int count = costPriceMapper.count1(sparePartVo);

        AllRecords allRecords = new AllRecords();
        allRecords.setPageIndex(sparePartVo.getPageIndex());
        allRecords.setPageSize(sparePartVo.getPageSize());
        allRecords.setTotalNumber(count);
        allRecords.setDataList(sparePartVoList);
        allRecords.resetTotalNumber(count);

        return ResultBuilderVo.success(allRecords);
    }


}